# -*- coding: utf-8 -*-
# flake8: noqa
import os
import sys

from qiniu import Auth, put_file, etag

# 需要填写你的 Access Key 和 Secret Key
access_key = sys.argv[3]
secret_key = sys.argv[4]

# 构建鉴权对象
q = Auth(access_key, secret_key)

# key 传到七牛后保存的文件名
# localfile 要上传文件的本地路径
def upload_one(bucket_name, localfile, key, expires):
    # 生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, expires)
    ret, info = put_file(token, key, localfile)
    assert ret['key'] == key
    assert ret['hash'] == etag(localfile)
    return ret

build_path = sys.argv[1]
app_name = sys.argv[2]
bucket_name = sys.argv[5]   # 要上传的空间
expires = 3600 * 24 * 360 * 30
for x in os.listdir(build_path):
    localfile = os.path.join(build_path, x)
    key = app_name + os.path.sep + os.path.split(localfile)[1]
    print 'start upload ' + localfile + ' to ' + key
    ret = upload_one(bucket_name, localfile, key, expires)
    print 'ret=' + `ret`

# for root, dirs, files in os.walk(build_path):
#     for file in files:
#         print os.path.join(file)